import { IotGateway } from "_common/entity";
import { createRequest, RequestMethod } from "_common/request";
import { AxiosInstance } from "axios";

/**
 * @Author: easten
 * @Created: 2024-07-23 15:29:46
 * @Description: 网关管理接口
 *
 */
export class ApiGateway {
  /**
   * @description 请求实例
   * @type {RequestMethod}
   * @memberof ApiDevice
   * */
  request: RequestMethod;
  url: string = "api/iot/gateway/";

  /**
   * @description 构造函数
   * @param config axios 配置
   */
  public constructor(instance: AxiosInstance) {
    // axios 实例化
    this.request = createRequest(instance);
  }

  /** 创建 */
  crete(data: IotGateway) {
    return this.request.post<boolean>(this.url + "", data);
  }

  /** 更新 */
  update(data: IotGateway) {
    return this.request.put<boolean>(this.url + "", data);
  }

  /** 删除 */
  delete(id: number) {
    return this.request.delete<boolean>(this.url + "" + id);
  }

  /** 获取信息 */
  getGatewayInfo(id: number) {
    return this.request.get<IotGateway>(this.url + "get-attr", { id });
  }

  /** 获取列表 */
  getGatewayList() {
    return this.request.get<IotGateway[]>(this.url + "get-list");
  }

  // 获取分页列表
  getPage(params: any) {
    return this.request.get<any>(this.url + "get-page", params);
  }

  // 获取tcp 配置
  getTcpConfig(id: number) {
    return this.request.get<any>(this.url + "get-tcp-config/" + id);
  }

  // 获取串口配置
  getSerialConfig(id: number) {
    return this.request.get<any>(this.url + "get-serial-config/" + id);
  }

  // 重启网关
  restart() {
    return this.request.post<any>(this.url + "restart");
  }

  // 启用，停用网关
  enable(id: number, enable: boolean) {
    return this.request.get<any>(this.url + "enable/" + id, {
      isEnable: enable,
    });
  }
}
